<?php

namespace app\app\controller;

use think\Db;

class Home extends Common
{
    //获取上方轮播图
    public function get_above_banner()
    {
        $where = get_pca();
        $where['type'] = 0;
        $where['status'] = 1;
        $data = Db::table('carousel')->field('url,image,url_type')->where($where)->order('id desc')->select();
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //获取下方轮播图
    public function get_under_banner()
    {
        $where = get_pca();
        $where['type'] = 1;
        $where['status'] = 1;
        $data = Db::table('carousel')->field('url,image,url_type')->where($where)->order('id desc')->select();
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //获取商家分类
    public function get_seller_category()
    {
        $where = get_pca();
        $data = Db::table('seller_category')->field('id as cate_id,title,image')->where($where)->order('id asc')->select();
        return json(['state' => 1, 'data' => $data, ' msg' => '获取成功']);
    }

    //获取广告图
    public function get_ad()
    {
        $where = get_pca();
        $where['type'] = 2;
        $data = [];
        $data['url'] = '';
        $data['image'] = '';
        $data['status'] = '';
        $data['time'] = '';
        if($x = Db::table('carousel')->field('url,image,status,time')->where($where)->order('id desc')->find()){
            $data = $x;
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //获取用户注册协议
    public function get_agreement()
    {
        $content = '<title>用户注册协议</title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
        return $content . Db::table('notice')->where('title', '用户注册协议')->value('content');
    }

    //获取关于我们
    public function get_about()
    {
        $content = '<title>关于我们</title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
        return $content . Db::table('notice')->where('title', '关于我们')->value('content');
    }

    //获取客服电话
    public function get_service_phone()
    {
        $uid = token_decode(req('token'));
        $aid = Db::table('seller')->where('id',$uid)->value('aid');
        $data = Db::table('service_phone')->where('aid',$aid)->value('service_phone');
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //用户使用帮助
    public function user_use_help()
    {
        $content = '<title>使用帮助</title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />';
        return $content . Db::table('notice')->where('title', '用户使用帮助')->value('content');
    }

    //版本更新
    public function check_update()
    {
        $this_version = req('version');
        $type = req('type');
        $msg['state'] = 0;
        $msg['data'] = null;
        $update_config = Db::table('update')->field('url,size,version,description,constraint')->where('type', $type)->order('time desc')->find();
        if (count($update_config) == 0) {
            $msg['data']['url'] = '';
            $msg['data']['size'] = '';
            $msg['data']['version'] = '';
            $msg['data']['description'] = '';
            $msg['data']['constraint'] = 0;
            $msg['msg'] = '当前已是最新版本';
        } else {
            if (str_replace('.', '', $this_version) < str_replace('.', '', $update_config['version'])) {
                $msg['state'] = 1;
                $msg['msg'] = '检测到新版本，请更新';
                $msg['data'] = $update_config;
            } else {
				$msg['data']['url'] = '';
				$msg['data']['size'] = '';
				$msg['data']['version'] = '';
				$msg['data']['description'] = '';
				$msg['data']['constraint'] = 0;
                $msg['msg'] = '当前已是最新版本';
            }
        }
        return json($msg);
    }

    //意见反馈
    public function send_opinion()
    {
        $uid = (int)token_decode(req('token'));
        $content = (string)req('content');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid)) {
            $msg['msg'] = '请登录后再使用该功能';
        } else {
            if (empty($content)) {
                $msg['msg'] = '请输入要反馈的内容';
            } else {
                $area = Db::table('user')->field('pid,cid,aid')->where('id', $uid)->find();
                $area['uid'] = $uid;
                $area['content'] = $content;
                $area['time'] = time();
                Db::table('opinion')->insert($area);
                $msg['state'] = 1;
                $msg['msg'] = '操作成功';
            }
        }
        return json($msg);
    }

    //获取已开通城市列表
    public function get_city_list()
    {
        $data = Db::table('province')->field('id as pid,name')->where('status', 1)->select();
        foreach ($data as $k => $v) {
            $children = Db::table('city')->field('id as cid,name')->where('status', 1)->select();
            foreach ($children as $k2 => $v2) {
                $children[$k2]['children'] = Db::table('area')->field('id as aid,name')->where('status', 1)->select();
            }
            $data[$k]['children'] = $children;
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //获取邀请送好礼
    public function invitation_description()
    {
        $uid = (int)token_decode(req('token'));
        $msg['state'] = 0;
        $msg['data'] = json('');
        if (empty($uid)) {
            $msg['msg'] = '请登录后再使用该功能';
        } else {
            $msg['data'] = [];
            $msg['state'] = 1;
            $where = get_pca();
            $where['title'] = '邀请送好礼';
            $data = Db::table('active')->field('description,status')->where($where)->find();
            $data['invitation'] = $uid;
            $list = Db::table('invitation')->where('uid', $uid)->select();
            foreach ($list as $k => $v) {
                $user = Db::table('user')->where('id', $v['in_id'])->find();
                $list[$k]['headimg'] = $user['headimg'];
                $list[$k]['name'] = $user['name'];
                unset($list[$k]['uid']);
                unset($list[$k]['in_id']);
            }
            $data['list'] = $list;
            $msg['data'] = $data;
            $msg['msg'] = '获取成功';
        }
        return json($msg);
    }

    //获取红包发放列表
    public function grab_red_packet_list()
    {
        $where = get_pca();
        $data = Db::table('redpacket_list')->field('id as red_id,seller_id,number,grab_number,full,minus')->where($where)->order('grab_number asc')->select();
        foreach ($data as $k => $v){
            if($v['seller_id'] == ''){
                $seller_name = '全部商家可用';
            }else{
                $seller_array = explode(',',$v['seller_id']);
                $count_seller = count($seller_array);
                $seller_name = '仅限';
                if($count_seller > 5){
                    $seller_name = '部分商家可用';
                }else{
                    foreach ($seller_array as $item){
                        if($item){
                            $seller_name .= Db::table('seller')->where('id',$item)->value('name').' ';
                        }
                    }
                    $seller_name .= '可用';
                }
            }
            $data[$k]['seller_name'] = $seller_name;
        }
        return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
    }

    //领取红包
    public function grab_red_packet()
    {
        $uid = (int)token_decode(req('token'));
        $red_id = (int)req('red_id');
        $msg['state'] = 0;
        $msg['data'] = '';
        if (empty($uid) || empty($red_id)) {
            $msg['msg'] = '参数错误';
        } else {
            $red_packet = Db::table('redpacket_list')->field('seller_id,number,grab_number,full,minus,indate,get_people')->where('id', $red_id)->find();
            if ($red_packet['grab_number'] >= $red_packet['number']) {
                $msg['msg'] = '手慢了一步，红包已经发放完啦~';
            } else {
                $people_array = explode(',', $red_packet['get_people']);
                if (in_array($uid, $people_array)) {
                    $msg['msg'] = '你已经领取过该红包了哦~';
                } else {
                    $grab_number = $red_packet['grab_number'] + 1;
                    unset($red_packet['grab_number']);
                    unset($red_packet['get_people']);
                    unset($red_packet['number']);
                    $red_packet['uid'] = $uid;
                    $red_packet['end_time'] = time() + ($red_packet['indate'] * 86400);
                    unset($red_packet['indate']);
                    $red_packet['description'] = '';
                    $red_packet['flag'] = 0;
                    Db::table('red_packet')->insert($red_packet);

                    if (empty($red_packet['get_people'])) {
                        $people = ',' . $uid . ',';
                    } else {
                        $people = $red_packet['get_people'] . $uid . ',';
                    }
                    $msg['state'] = 1;
                    Db::table('redpacket_list')->where('id', $red_id)->update(['get_people' => $people, 'grab_number' => $grab_number]);
                    $msg['msg'] = '领取成功';
                }
            }
        }
        return json($msg);
    }

    //配送员信息
    public function express_info()
    {
        $uid = token_decode(request()->request('token'));
        $business = request()->request('business');
        $msg['state'] = 0;
        $msg['data'] = [];
        if (empty($uid)) {
            $msg['msg'] = '参数错误';
        }else{
            if($business == 1 || $business == 0){
                if($business != 0){
                    $express = Db::table('express')->where('id',$uid)->find();
                    //推送新单给自己
                    $new_order_count = Db::table('order')->where(['status'=>2,'advance_receive'=>1,'aid'=>$express['aid']])->count();
                    $oneself_order = Db::table('order')->where(['express_id'=>$uid,'status'=>['between','2,4']])->count();
                    if($new_order_count > 0 && $oneself_order == 0){
                        push(md5($uid),json_encode(['type' => 5, 'message' => '有新的订单可以抢单哦~', 'parameters' =>'']),'express');
                    }
                }
                Db::table('express')->where('id',$uid)->update(['business'=>$business]);
            }
            $msg['data'] = Db::table('express')->field('name,business')->where('id',$uid)->find();
            $msg['state'] = 1;
            $msg['msg'] = '操作成功';
        }
        return json($msg);
    }
	
	//获取城市列表
	public function getcity(){
		$citylist = Db::table('city')->where('status',1)->field('id,capital,name')->order('capital asc')->select();
		$return = array();
		$return['state'] = 1;
		$return['msg'] = '';
		$return['data'] = $citylist;
		return json($return);
	}
	
	//获取桌台信息
    public function get_desk_info()
    {
        $code = req('code');
        $seller_id = req('seller_id');
		if($code){
			$where['code'] = $code;
			$where['seller_id'] = $seller_id;
			$data = Db::table('desk')->field('id,title,seller_id')->where($where)->find();
			return json(['state' => 1, 'data' => $data, 'msg' => '获取成功']);
		}else{
			return json(['state' => 0, 'data' => [], 'msg' => '参数错误']);
		}
    }
}